package com.iwhalecloud.bss.kite.cucc.order.operate;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts2;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.iwhalecloud.bss.kite.cucc.order.util.AcceptUtils;
import com.iwhalecloud.bss.kite.dataservice.entity.OrderItem;
import com.iwhalecloud.bss.kite.dataservice.entity.order.OrderItemChange;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.order.client.api.operate.ICreateWithdrawOrderItemService;
import com.iwhalecloud.bss.kite.order.client.dto.OrderContainer;
import com.iwhalecloud.bss.kite.order.client.dto.WithdrawOrderRequest;
import com.ztesoft.bss.base.staticdata.CoreDataInteract;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class CreateWithdrawOrderItemService implements ICreateWithdrawOrderItemService {

    @Override
    public OrderItem createWithdrawOrderItems(WithdrawOrderRequest request, String orderItemId,
            String newOrderItemId, String newAskId, String undoReason, String remark, String staffId) {
        OrderItem orderItem = OrderContainer.ofRequest(request).getOrderItem(orderItemId);
        OrderItem item = KiteBeanUtils.transform(orderItem, OrderItem.class);
        item.setOldOrderItemId(orderItemId);
        item.setAskId(newAskId);
        item.setOrderItemId(newOrderItemId);
        item.setCustWorksheetId(newOrderItemId);
        item.setOperType(CoreDataInteract.EVT_C_0003_1200);
        item.setCustOrderId(request.getCustOrderId());
        item.setUndoFlag(KeyConsts.UNDO_FLAG_T);
        item.setFlowId(null);
        item.setUndoReason(undoReason);
        item.setCreateStaff(staffId);
        item.setRemark(remark);
        //  更新订单项
        item.setOrderItemId(newOrderItemId);
        item.setAskId(newAskId);

        item.setSkipOpen(KeyConsts2.SKIP_OPEN_F);

        item.setAcceptDate(request.getAcceptDate());
        item.setStatusDate(request.getAcceptDate());
        item.setCreateDate(request.getAcceptDate());
        OrderContainer.ofRequest(request).addOrderItem(item);
        request.getOrderContainer().getOrderItemMap().remove(orderItemId);
        return item;
    }

    @Override
    public void createWithdrawOrderItemChanges(WithdrawOrderRequest request, String orderItemId, String staffId, OrderItem newIterm) {
        List<OrderItemChange> changes = request.getOrderContainer().getOrderItemChangeMap().get(orderItemId);
        Map<String, List<OrderItemChange>> orderItemChangeMap = request.getOrderContainer().getOrderItemChangeMap();
        List<OrderItemChange> changesItem = new ArrayList<>();
        if (KiteListUtils.isNotEmpty(changes)) {
            Iterator<OrderItemChange> it = changes.iterator();
            while (it.hasNext()) {
                OrderItemChange change = it.next();
                // 更新变动的客户订单，时间，主键
                change.setCustOrderId(newIterm.getCustOrderId());
                change.setOrderItemId(newIterm.getOrderItemId());
                change.setHandleTime(new Date());
                change.setChgId(SeqUtils.getNext(TableConsts.OrderItemChange));

                // TODO 修改实例actiontype为M
                if (ActionType.A.name().equals(change.getActionType())) {
                    change.setActionType(ActionType.M.name());
                }
                change.setInstStateChg(AcceptUtils.withDrawInstStateChg(change.getInstStateChg()));

                String newValue = change.getFieldValue();
                String oldValue = change.getOldFieldValue();
                change.setFieldValue(oldValue);
                change.setOldFieldValue(newValue);
                change.setActionType(ActionType.M.name());
                changesItem.add(change);

            }
            orderItemChangeMap.put(newIterm.getOrderItemId(), changesItem);
            request.getOrderContainer().getOrderItemChangeMap().remove(orderItemId);
        }
    }
}
